<?php

namespace App\Repository\Repositories;

use App\Http\Controllers\Controller;
use App\Http\Serve\Es;
use App\Models\StoreCoupon;
use App\Models\StoreCouponIssue;
use App\Models\StoreCouponIssueUser;
use App\Models\StoreCouponUser;
use App\Models\StoreProduct;
use App\Repository\RepositoryInterface\CouponInterface;
use Illuminate\Support\Facades\DB;

class CouponRepositories extends Controller implements CouponInterface
{
    protected $storeCoupon;

    public function __construct(StoreCoupon $storeCoupon)
    {
        $this->storeCoupon = $storeCoupon;
    }

    public function modification($discountData, $id)
    {
        try {
            $updateRes = StoreCoupon::modification($discountData, $id);
            if ($updateRes) {
                $es = new Es();
                if (!$es->exists_index('discount')) {
                    $es->create_index('discount');
                }
                // 修改es的数据
                $es->update_doc($id, 'discount', '_doc', ['doc' => $discountData]);
            }
            return parent::success('success', $discountData);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function listview($where)
    {
        try {
            $discountData = StoreCoupon::search($where);
            return parent::success('success', $discountData);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function delete($id)
    {
        DB::beginTransaction();
        try {
            if (!is_numeric($id)) {
                throw new \ErrorException('删除优惠券参数错误');
            }
            $deleteRes = StoreCoupon::discountDeleted($id);
            if ($deleteRes) {
                $es = new Es();
                $es->delete_doc($id, 'discount', '_doc');
                StoreCouponIssue::discountDeleted($id);
            }

            DB::commit();
            return parent::success('success');
        } catch (\Exception $exception) {
            DB::rollBack();
            return parent::error($exception->getMessage());
        }
    }

    public function create($discountData)
    {
        try {
            $getId = StoreCoupon::create($discountData);
            if ($discountData) {
                $es = new Es();
                if (!$es->exists_index('discount')) {
                    $es->create_index('discount');
                }
                $es->add_doc($getId, $discountData, 'discount', '_doc');
            }
            return parent::success('添加成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function read($id)
    {
        try {
            if (!is_numeric($id)) {
                throw new \ErrorException('删除优惠券参数错误');
            }
            $discountData = StoreCoupon::readStoreCouponRead($id);
            return parent::success('success', $discountData);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function attachedCreate($attachedData)
    {
        try {
            StoreCouponIssue::create($attachedData);
            return parent::success('success');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function attachedUpdate($attachedData, $id)
    {
        try {
            StoreCouponIssue::modification($attachedData, $id);
            return parent::success('success');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function userList($id)
    {
        try {
            $userData = StoreCouponIssueUser::userStoreIssue($id);
            return parent::success('success', $userData);
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function useradd($id, $couponId)
    {
        try {
            if (!is_numeric($id) || !is_numeric($couponId)) {
                throw new \ErrorException('参数不正确');
            }
            StoreCouponIssueUser::useradd($id, $couponId);
            return parent::success('success');
        } catch (\ErrorException $exception) {
            return parent::error($exception->getMessage());
        }
    }

    public function changeStatus($id, $status)
    {
        $status = StoreCoupon::changeStatus($id, $status);
        return parent::success($status);
    }

    public function getGoods()
    {
        $goods = StoreProduct::all();
        return parent::success('success', $goods);
    }

    public function checkStatus($data)
    {
        try {
            DB::beginTransaction();
            foreach ($data['id'] as $k => $v) {
                StoreCouponUser::where('id', $v)->update(['status' => 1]);
            }
            DB::commit();
            return parent::success('success');
        } catch (\Exception $exception) {
            DB::rollBack();
            return parent::error('error');
        }
    }

    public function productDiscount($id)
    {
        try {
            if (!is_numeric($id) || $id <= 0) {
                return parent::error('参数不正确');
            }
            $product = StoreCoupon::getProductDiscount($id);
            return parent::success("success", $product);
        } catch (\Exception $exception) {
            return parent::error("error");
        }

    }

}
